<?php

namespace App\Admin\Controllers;

use App\Models\Classroom;
use App\Models\Crouse_time;
use App\Models\Squad;
use App\Models\Student;
use App\Models\Student_attendance_record;
use App\Models\Attendance_cycle;
use App\Models\Attendance_report;
use Encore\Admin\Auth\Database\Administrator;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use App\Admin\Extensions\ExcelExpoter;
use Maatwebsite\Excel\Facades\Excel;
use Encore\Admin\Show;
use Illuminate\Http\Request;
use DB;

class Student_attendance_recordController  extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header(trans('admin.attendance_report'));
            $content->description(trans('admin.list'));

            $content->body($this->grid());
        });
    }

    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $student_id = Student_attendance_record::find($id)->student_id;
            $student_en_name = Student::find($student_id)->en_name;
            $content->header($student_en_name.'的考勤');
            //$content->header(trans('admin.test_report'));
            $content->description(trans('admin.edit'));
            $content->body($this->form()->edit($id));
        });
    }

    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header(trans('admin.attendance_report'));
            $content->description(trans('admin.create'));

            $content->body($this->form());
        });
    }
    
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Student_attendance_record::class, function (Grid $grid) {
            $grid->id('序号')->sortable();
            $grid->student_id(trans('admin.en_name'))->display(function($student_id){
                return Student::find($this->student_id)->en_name;
            });
            $grid->attendance_status(trans('admin.attendance_status'))->display(function ($attendance_status) {
                if($this->attendance_status==1){
                    return "<span class='label bg-green'>正常</span>";
                }elseif ($this->attendance_status==2) {
                    return "<span class='label bg-orange'>请假</span>";
                }elseif ($this->attendance_status==3) {
                    return "<span class='label bg-blue'>迟到</span>";
                }elseif ($this->attendance_status==4) {
                    return "<span class='label bg-red'>缺勤</span>";
                }
            })->sortable();
            $grid->attendance_desc(trans('admin.attendance_desc'))->display(function($attendance_desc) {
                return str_limit($attendance_desc, 30, '...');
            })->editable('textarea');
            $grid->actions(function ($actions) {
                $actions->disableDelete();
                //$actions->disableEdit();
                $actions->disableView();
            });
            $grid->disableCreateButton();
            //$grid->disableActions();
            //禁用导出按钮
            //$grid->disableExport();
            $filename="考勤记录";
            $grid->exporter(new ExcelExpoter($grid,$filename));
            
            $grid->tools(function ($tools) { 
                //关闭批量删除 
                $tools->batch(function ($batch) { 
                    $batch->disableDelete(); 
                });
            });
            //$grid->disableFilter();//禁用查询
        });
    }

    protected function form()
    { 
        $form = new Form(new Student_attendance_record());
        $form->display('id', 'ID');
        $form->select('attendance_status', trans('admin.attendance_status'))->options([1=>'正常',2=>'请假',3=>'迟到',4=>'缺勤',])->rules('required')->addElementLabelClass('must-input');
        $form->textarea('attendance_desc',trans('admin.attendance_desc'));
        
            
        //去掉添加/编辑表单的重置按钮
        $form->disableReset();
        $form->tools(function (Form\Tools $tools) {
            // 去掉`列表`按钮
            $tools->disableList();
            // 去掉`删除`按钮
            $tools->disableDelete();
            // 去掉`查看`按钮
            $tools->disableView();
        });
        $form->saved(function (Form $form) {
            $attendance_report_id = $form->model()->attendance_report_id;
            // 跳转页面
            return redirect('/admin/attendance_report/'.$attendance_report_id);
            });
        return $form;
    }
}
